package system

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzsystemappversion"
	"private-domain-overseas-service/tools/ent/predicate"
)

type SystemVersionDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSystemVersionDao(ctx context.Context, svcCtx *svc.ServiceContext) *SystemVersionDao {
	return &SystemVersionDao{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// SystemVersionCreate - 添加版本
func (l *SystemVersionDao) SystemVersionCreate(version *ent.HzSystemAppVersion, tx *ent.Tx) error {
	clientDao := l.svcCtx.Orm.HzSystemAppVersion
	if tx != nil {
		clientDao = tx.HzSystemAppVersion
	}
	err := clientDao.Create().
		SetAppPackageName(version.AppPackageName).
		SetAppVersion(version.AppVersion).
		SetAppPlatform(version.AppPlatform).
		SetAppVersionTitle(version.AppVersionTitle).
		SetAppForceUpdateType(version.AppForceUpdateType).
		SetAppHotUpdateType(version.AppHotUpdateType).
		SetAppPublishStatus(version.AppPublishStatus).
		SetAppSdkUpdateType(version.AppSdkUpdateType).
		SetAppUpdateDownloadURL(version.AppUpdateDownloadURL).
		SetAppVersionDescription(version.AppVersionDescription).
		SetCreatedAt(version.CreatedAt).
		SetUpdateAt(version.UpdateAt).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// SystemVersionUpdate - 修改版本
func (l *SystemVersionDao) SystemVersionUpdate(version *ent.HzSystemAppVersion, tx *ent.Tx) error {
	clientDao := l.svcCtx.Orm.HzSystemAppVersion
	if tx != nil {
		clientDao = tx.HzSystemAppVersion
	}
	err := clientDao.UpdateOneID(version.ID).
		SetAppPackageName(version.AppPackageName).
		SetAppVersion(version.AppVersion).
		SetAppPlatform(version.AppPlatform).
		SetAppVersionTitle(version.AppVersionTitle).
		SetAppForceUpdateType(version.AppForceUpdateType).
		SetAppHotUpdateType(version.AppHotUpdateType).
		SetAppPublishStatus(version.AppPublishStatus).
		SetAppSdkUpdateType(version.AppSdkUpdateType).
		SetAppUpdateDownloadURL(version.AppUpdateDownloadURL).
		SetAppVersionDescription(version.AppVersionDescription).
		SetUpdateAt(version.UpdateAt).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// SystemVersionDelete - 删除版本
func (l *SystemVersionDao) SystemVersionDelete(version *ent.HzSystemAppVersion, tx *ent.Tx) error {
	clientDao := l.svcCtx.Orm.HzSystemAppVersion
	if tx != nil {
		clientDao = tx.HzSystemAppVersion
	}
	_, err := clientDao.UpdateOneID(version.ID).
		SetDeleteAt(version.DeleteAt).
		Save(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// SystemVersionConditionsFirst - 根据条件查询单个版本信息
func (l *SystemVersionDao) SystemVersionConditionsFirst(conditions []predicate.HzSystemAppVersion) (*ent.HzSystemAppVersion, error) {
	version, err := l.svcCtx.Orm.HzSystemAppVersion.Query().
		Where(conditions...).
		First(l.ctx)
	if err != nil {
		return nil, err
	}
	return version, nil
}

// SystemVersionConditionsAll - 根据条件查询多个版本信息
func (l *SystemVersionDao) SystemVersionConditionsAll(conditions []predicate.HzSystemAppVersion) ([]*ent.HzSystemAppVersion, error) {
	version, err := l.svcCtx.Orm.HzSystemAppVersion.Query().
		Where(conditions...).
		All(l.ctx)
	if err != nil {
		return nil, err
	}
	return version, nil
}

// SystemVersionConditionsAllByPage - 分页查询版本信息
func (l *SystemVersionDao) SystemVersionConditionsAllByPage(conditions []predicate.HzSystemAppVersion, current, pageSize int64) ([]*ent.HzSystemAppVersion, int64, error) {
	query := l.svcCtx.Orm.HzSystemAppVersion.Query().Where(conditions...)

	total, err := query.Count(l.ctx)
	if err != nil {
		return nil, 0, err
	}

	records, err := query.
		Offset(int((current - 1) * pageSize)).
		Order(ent.Desc(hzsystemappversion.FieldID)).
		Limit(int(pageSize)).
		All(l.ctx)
	if err != nil {
		return nil, 0, err
	}

	return records, int64(total), nil

}
